<template>
  <div class="topNav">
    <div class="content warp">
      <div class="indexPage" v-if="!this.$route.meta.hiddenLogo">
        <h1 style="color:#fff;font-size:18px">优易捷科技电商平台</h1>
        <router-link to="/">
          <!-- <icon-svg icon-class="logo" /> -->
          <!-- <img src="static\image\nav-logo.png" alt=""> -->
          <!-- <img src="static\image\logo.png" alt="">
            <img src="https://vpigshop.oss-cn-hangzhou.aliyuncs.com/shop/2023-04-10/087e3ac9da9c4c66be3203bdaaf56adb_超清logo00.jpg" alt=""> -->
        </router-link>
      </div>
      <div v-else>
        <!-- <h1 style="color:#fff">优易捷科技电商平台</h1> -->

      </div>
      <div class="right">
        <!-- <div class="item">
          <a class="hover-item" @click="applySettle">申请入驻</a>
        </div> -->
        <!-- <div class="item">
          <a class="hover-item" @click="consultationPaid">付费咨询</a>
        </div> -->
        <div class="item">
          <a class="hover-item" @click="goToIndex">首页</a>
        </div>
        <div class="item">
          <a class="hover-item" target="_blank" @click="consultationPaid">商家入驻</a>
        </div>
         <div class="item">
          <a class="hover-item" target="_blank" @click="consultationPaySeekChange">付费咨询</a>
        </div>
        <div class="item">
          <a class="hover-item" @click="goToUserOrder">我的订单</a>
        </div>
        <!-- 购物车 -->
        <div class="item item-select">
          <span class="hover-item" @click="goToShopCart">
            <i class="iconfont icon-shoppingCart"></i>购物车
            <span v-if="isShowNum">
              (<b v-text="cartNumber === 0 ? '0' : cartNumber"></b>)
            </span>
            <i class="ico-arrow el-icon-arrow-down"></i>
          </span>
          <div v-if="!isLogin" class="cart-box cart-no-login">
            <a class="loginBtn" @click="loginBtn">登录</a>
            <p class="p">登录后查看商品</p>
          </div>
          <div v-else-if="shoppingCart.length === 0" class="cart-box cart-no-data">
            <img src="static\image\gouwuche_no.webp" alt="">
            <p class="p">购物车还没有商品，赶紧选购吧！</p>
          </div>
          <div v-else class="cart-box cart-list">
            <div class="cartItem" v-for="shop in shoppingCart" :key="shop.shopId">
              <dl v-for="(item, index) in shop.skus" :key="index" v-show="index<3">
                <dt>
                  <div class="cartImgBox" style="width: 42px;height: 42px;background: #F5F5F5;" @click="$router.push('/shopping-trolley')">
                    <img :src="item.image" alt="">
                  </div>
                </dt>
                <dd>
                  <h4 class="h4" @click="$router.push('/shopping-trolley')">{{item.productName}}</h4>
                  <p>{{item.SKU}}</p>
                  <span>￥{{item.price}}</span>
                  <a class="btn-del" @click="deleteCartItem(item)"><i class="el-icon-delete" /></a>
                </dd>
              </dl>
            </div>
            <a class="btn-view-all" @click="goToShopCart" v-if="cartNumber>3">查看全部</a>
          </div>
        </div>
        <!-- 我的 -->
        <div class="item item-select">
          <span class="hover-item"><i class="iconfont icon-user"></i>我的<i class="ico-arrow el-icon-arrow-down"></i></span>
          <div class="cart-box user-box">
            <div v-if="!isLogin">
              <div class="loginBtn" @click="loginBtn">登录账号</div>
            </div>
            <div v-if="isLogin" class="info">
              <div class="avatar" @click="goToUserInfo">
                <img :src="userInfoData.headImage" alt="">
              </div>
              <div class="vip">
                <div class="top">
                  <div class="level">{{ userInfoData.memberLevelName}} </div>
                  <div class="logout" @click="outLoginBtn">退出登录</div>
                </div>
                <div class="bottom">
                  <p>会员成长值：<span>{{ userInfoData.growth }}/{{ userInfoData.nextLevelGrowth }}</span></p>
                  <el-progress :show-text="false" :percentage="parseInt(userInfoData.growth / userInfoData.nextLevelGrowth) * 100 || 0">
                  </el-progress>
                  <!-- <el-progress
                    :show-text="false"
                    :percentage="70"></el-progress> -->
                </div>
              </div>
            </div>
            <ul v-if="isLogin" class="personal">
              <li @click="goToUserAddress">
                <icon-svg style="font-size: 54px;" icon-class="nav-addres" />
                收货地址
              </li>
              <li @click="gotoUserCoupon">
                <icon-svg style="font-size: 54px;" icon-class="nav-coupon" />
                券包
              </li>
              <li @click="goToAfterOrder">
                <icon-svg style="font-size: 54px;" icon-class="nav-aftersale" />
                售后订单
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
    <el-dialog title="提示" :visible.sync="outLoginDialog" width="30%">
      <span>确定退出登录吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="outLoginDialog = false">取 消</el-button>
        <el-button class="checkOut" @click="outLogin">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog custom-class="pay_dialog"  title="" :visible.sync="consultationPaidDialog" width="530px" >
      <!-- title插槽 -->
      <!-- <template slot="title">
      </template> -->
      <div class="pay_content">
        <img class="pay_img" src="/static/image/payConsult/1.png" alt="">
        <div class="enter">在线入驻</div>
        <div class="pay_text">
          <div class="pay_text_item">
            <img src="/static/image/payConsult/2.svg" alt="">
            <div class="t">即时性</div>
          </div>
          <div class="pay_text_item">
            <img src="/static/image/payConsult/3.svg" alt="">
            <div class="t">强互动性</div>
          </div>
          <div class="pay_text_item">
            <img src="/static/image/payConsult/4.svg" alt="">
            <div class="t">个性化</div>
          </div>
        </div>
        <div class="pay_but" @click="submit">￥19.9 立即支付</div>
        <div class="bom_txt">
          您好，个人商户可享受免费入驻服务；个体工商户、企业商户及其他组织需先完成付费，填写相关信息后即可入驻。
        </div>
      </div>
      <!-- <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit">提交</el-button>
        <el-button @click="consultationPaidDialog=false">取消</el-button>
      </span> -->
    </el-dialog>
    <el-dialog custom-class="qrcode_dialog" :visible.sync="payDialog" width="400px" >
      <div>
          <img class="money_img" src="static/image/pay.jpg" alt="">
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="finish">完成</el-button>   </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters, mapMutations } from 'vuex'
import Cookie from 'js-cookie'
import { Encrypt } from '@/util/secret'

import { getUserInfo } from '@/api/user/user.js'
import { getGongGaoAll } from '@/api/user/notice.js'
import { getCartList, deleteCartItem } from '@/api/user/cart.js'
import { settled } from '@/api/nav.js'

export default {
  name: 'siteNav',
  props: {
    componentContent: {
      type: Object
    }
  },

  computed: {
    ...mapGetters(['userInfo', 'shoppingCart', 'cartNumber'])
  },
  data() {
    return {
      allowJump: false,
      formData: {
        title: '',
        content: ''
      },
      consultationPaidDialog: false,
      payDialog: false,
      // ImproveInfo: false,
      userInfoData: '',
      searchVal: '宝贝',
      keyword: '',
      isShowOut: false,
      menuOpen: false,
      isLogin: false,
      outLoginDialog: false,
      isShowNum: false,
      deleteOpt: {
        visiable: false
      },
      proList: [],
      noticeList: []
    }
  },
  watch: {
    '$store.state.isLogin': {
      handler(nVal, oVal) {
        this.isLogin = nVal
        this.isShowNum = nVal
        if (this.isLogin) {
          this.getUserInfoData()
          this.getTrolleyList()
        }
      }
    }
    // '$store.state.userInfo.cardId': {
    //   handler(nVal, oVal) {
    //     if (nVal) {
    //       this.perfectInfo()
    //     }
    //   },
    //   immediate: true
    // }
  },
  created() {
    if (Cookie.get('token')) {
      this.isLogin = true
      this.isShowNum = true
      this.getTrolleyList()
      this.getGongGao()
      this.getUserInfoData()
    }
  },
  methods: {
    // emitImproveInfo(e) {
    //   this.ImproveInfo = e
    // },
    // perfectInfo() {
    //   if (!this.userInfo.reName || !this.userInfo.unitName) {
    //     this.ImproveInfo = true
    //   }
    // },
    ...mapMutations({
      setSearchObj: 'SET_SEARCHOBJ',
      setUserInfo: 'SET_USERINFO',
      setCartNumber: 'SET_CARTNUMBER',
      setShoppingCart: 'SET_SHOPPINGCART',
      setNewAddress: 'SET_NEWADDRESS',
      setAreaCode: 'SET_AREACODE',
      setNoticeId: 'SET_NOTICEID',
      IS_LOGIN: 'IS_LOGIN'
    }),
    submit() {
        this.payDialog = true
    },
    consultationPaid() {
      this.settMerchant() //直接跳转商家入驻
      // return
      // if (this.allowJump) {
      //   this.settMerchant()
      // } else {
      //   this.consultationPaidDialog = true
      // }
    },
    settMerchant() {
      let url = window.location.origin
      // console.log(url)
      window.open(url + '/settled-merchant/#/', '_blank')
      // window.location.href = url + '/settled-merchant/#/'
      // 这是重新加载按钮的点击事件
      // window.location.reload()
    },
    finish() {
      this.allowJump = true
      this.consultationPaidDialog = false
      this.payDialog = false
      this.formData = {
        title: '',
        content: ''
      }
      this.$message({
          message: '提交成功',
          type: 'success',
        })
    },

    searchCommand(command) {
      this.searchVal = command
    },
    // 查找
    searchPro() {
      this.setSearchObj({
        keyword: this.keyword,
        searchVal: this.searchVal
      })
      if (this.$route.name !== 'search') {
        this.$router.push({
          path: '/activity/search'
        })
      }
    },
    // 个人信息
    async getUserInfoData() {
      const response = await getUserInfo()
      const res = response.data
      if (res.code === '200') {
        this.setUserInfo(res.data)
        this.userInfoData = res.data
      } else {
        this.$message.error('获取个人信息失败！')
      }
    },
    // 购物车列表
    async getTrolleyList() {
      const response = await getCartList()
      const res = response.data
      let cartNumber = 0
      if (res.data && res.data.length) {
        res.data.forEach(j => {
          cartNumber = cartNumber + j.skus.length
        })
      }
      this.setShoppingCart(res.data)
      this.setCartNumber(cartNumber)
    },
    // 公告
    async getGongGao() {
      const response = await getGongGaoAll()
      const res = response.data
      if (res.code === '200') {
        this.noticeList = res.data
      } else {
        this.$message.warning(res.message)
      }
    },
    // 删除单个商品
    async deleteCartItem(item) {
      let ids = []
      ids.push(item.skuId)
      await deleteCartItem({ ids })
      let id = item.skuId
      this.shoppingCart.map(j => {
        j.skus.map(k => {
          if (k.skuId === id) {
            j.skus.splice(
              j.skus.findIndex(item => item.skuId === id),
              1
            )
          }
        })
      })
      this.setCartNumber(this.cartNumber - 1)
      this.getTrolleyList()
      this.$message.success('删除成功')
    },
    outLoginBtn() {
      this.outLoginDialog = true
    },
    outLogin() {
      this.outLoginDialog = false
      Cookie.remove('token')
      this.setUserInfo({})
      this.setCartNumber('')
      this.setShoppingCart({})
      this.setNewAddress({})
      this.setAreaCode([])
      // this.$router.push({path: '/login', query: {type: 2}})
      // this.$router.push({ path: '/index' })
      this.$store.commit('IS_LOGIN', false) // 清除登录状态
      this.isLogin = false
      this.isShowNum = false
      location.reload()
    },
    // 申请入驻
    applySettle() {
      let token
      if (Cookie.get('token')) {
        token = Encrypt(Cookie.get('token'))
      } else {
        this.$store.commit('SHOW_LOGIN')
        return
      }
      let username = encodeURI(this.userInfo.name)
      let userInfo = encodeURI(username)
      // let test = Decrypt(token)
      window.open(`${settled}?username=${userInfo}&user=${token}`)
    },
    goToUserInfo() {
      if (Cookie.get('token')) {
        this.$router.push({ path: '/userInfo' })
      } else {
        this.$store.commit('SHOW_LOGIN')
      }
    },
    goToIndex() {
      //  this.$router.push('/store')
      //  return
      this.$router.push({
        path: '/index'
        // query: {
        //   shopId: this.$store.state.collectData.shopId || 225
        // }
      })
    },
    goToUserOrder() {
      if (Cookie.get('token')) {
        this.$router.push({ path: '/myOrder' })
      } else {
        this.$store.commit('SHOW_LOGIN')
      }
    },
    goToAfterOrder() {
      if (Cookie.get('token')) {
        this.$router.push({ path: '/orderAfterSale' })
      } else {
        this.$store.commit('SHOW_LOGIN')
      }
    },
    // 商家入驻
    consultationPaySeekChange() {
      this.$router.push({ path: '/consultationPaySeek' })
    },
    loginBtn(type) {
      // console.log(type)
      // this.$store.commit('SHOW_LOGIN')
      this.$router.push({ path: '/login', query: { type: type } })
    },
    goToUserAddress() {
      if (Cookie.get('token')) {
        this.$router.push({ path: '/signingAddress' })
      } else {
        this.$store.commit('SHOW_LOGIN')
      }
    },
    gotoUserCoupon() {
      if (Cookie.get('token')) {
        this.$router.push({ path: '/couponPackage' })
      } else {
        this.$store.commit('SHOW_LOGIN')
      }
    },
    goToShopCart() {
      if (Cookie.get('token')) {
        this.$router.push({ path: '/shopping-trolley' })
      } else {
        this.$store.commit('SHOW_LOGIN')
      }
    },
    hover() {
      this.isShowOut = true
    },
    // 跳转到公告详情
    goToDetail(id) {
      this.$router.push({
        path: '/activity/notificationDetails',
        query: {
          id: id
        }
      })
      this.setNoticeId(id)
    }
  }
}
</script>

<style lang="scss" scoped>
$lightBlack: #666666;
$navSearchHeight: 39px;

.topNav {
  height: 50px;
  background: #333333;
  .content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    .search {
      width: 430px;
      max-width: 100%;
      height: $navSearchHeight;
      margin: 5px;
      border: 1px solid $lightBlack;
      border-radius: 0px;
      overflow: hidden;
      position: relative;
      display: flex;
      .searchSelect {
        margin: 10px 10px 10px 0;
        padding: 0 12px;
        border-right: 1px solid #e5e5e5;
        >>> .el-dropdown-link {
          font-size: 16px;
          color: #fff;
        }
      }
      .searchRight {
        flex: 1;
        input {
          width: 100%;
          height: $navSearchHeight;
          color: #cccccc;
          padding: 0 60px 0 15px;
          font-size: 14px;
          box-sizing: border-box;
          background-color: $lightBlack;
        }
      }
      .btn {
        width: 25px;
        height: 100%;
        padding-right: 12px;
        background-color: $lightBlack;
        color: #fff;
        line-height: $navSearchHeight;
        text-align: center;
        cursor: pointer;
      }
    }
    .indexPage {
      .svg-icon {
        margin-left: 12px;
        font-size: 40px;
        cursor: pointer;
      }
      img {
        margin-top: 4px;
        width: 45px;
        height: 42px;
      }
    }
    .right {
      display: flex;
      .item {
        padding: 0 15px;
        display: inline-block;
        color: #ffffff;
        line-height: 50px;
        span {
          cursor: pointer;
          b {
            color: $mainGlod;
            padding: 0 2px;
          }
        }
        .iconfont {
          margin-right: 5px;
        }
        .icon-user {
          font-size: 14px;
        }
        &.item-select {
          padding-right: 33px;
          position: relative;
          .ico-arrow {
            position: relative;
            position: absolute;
            right: 15px;
            top: 5px;
            margin-right: 0;
            // margin: 0 3px;
            line-height: 43px;
          }
        }
        &:hover {
          background-color: #fff;
          .cart-no-data,
          .cart-no-login {
            display: flex;
          }
          .cart-list,
          .user-box {
            display: block;
          }
          .hover-item {
            color: $mainGlod;
          }
        }
      }
    }
    .cart-box {
      position: absolute;
      top: 49px;
      // top: 100%;
      background-color: #fff;
      z-index: 999;
      border-radius: 5px;
      box-shadow: 0px 0px 24px 3px rgba(0, 0, 0, 0.06);
      .cartImgBox {
        img {
          width: 100%;
        }
      }
      &.cart-no-login {
        min-width: 200px;
        left: -100px;
        height: 160px;
        display: none;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        .btn {
          display: inline-block;
          padding: 0 15px;
          height: 26px;
          line-height: 26px;
          text-align: center;
          color: #fff;
          background-color: $mainColor;
          border-radius: 4px;
          margin-bottom: 40px;
        }
        .p {
          color: #666666;
          line-height: 1em;
        }
      }
      &.cart-no-data {
        width: 264px;
        height: 110px;
        left: 50%;
        margin-left: -132px;
        display: none;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        color: #666;
      }
      &.cart-list {
        width: 348px;
        left: 50%;
        margin-left: -174px;
        display: none;
        padding: 10px;
        .cartItem {
          border-bottom: 1px solid #f5f5f5;
          display: flex;
          flex-direction: column;
          align-items: center;
        }
        dl {
          flex: 1;
          width: 100%;
          height: 70px;
          margin: 5px 0;
          dt {
            float: left;
            cursor: pointer;
          }
          dd {
            margin-left: 54px;
            position: relative;
            padding-right: 60px;
            min-height: 42px;
            line-height: 21px;
            h4 {
              color: #333;
              cursor: pointer;
              font-size: 14px;
              @include ellipsis;
            }
            p {
              color: #333;
              font-size: 14px;
              @include ellipsis;
            }
            span {
              color: #c83732;
              position: absolute;
              top: 0;
              right: 0;
              font-size: 14px;
            }
            .btn-del {
              color: #333;
              position: absolute;
              top: 20px;
              right: 0;
              font-size: 14px;
            }
          }
        }
        .btn-view-all {
          display: block;
          margin: auto;
          // float: right;
          padding: 0 15px;
          height: 26px;
          line-height: 26px;
          text-align: center;
          color: #fff;
          background-color: $mainGlod;
          border-radius: 4px;
          margin-top: 10px;
          cursor: pointer;
        }
      }
      &.user-box {
        width: 400px;
        top: 48px;
        left: -315px;
        // padding: 18px 0;
        display: none;
        .info {
          display: flex;
          margin: 15px;
          height: 60px;
          .avatar {
            background-color: #666666;
            cursor: pointer;
            img {
              width: 54px;
              height: 54px;
            }
          }
          .vip {
            margin-left: 10px;
            flex: 1;
            cursor: default;
            .top {
              height: 35px;
              display: flex;
              justify-content: space-between;
              .level {
                display: block;
                width: 86px;
                height: 22px;
                color: $mainGlod;
                position: relative;
                top: -15px;
              }
              .logout {
                width: 115px;
                height: 30px;
                line-height: 30px;
                text-align: center;
                font-size: 14px;
                font-family: Microsoft YaHei;
                color: $mainGlod;
                background: #333333;
                box-shadow: 0px 5px 10px rgba(0, 0, 0, 0.16);
                border-radius: 3px;
                cursor: pointer;
              }
            }
            .bottom {
              p {
                font-size: 12px;
                line-height: 16px;
                color: #999999;
                span {
                  color: $mainGlod;
                }
              }
            }
          }
        }
        .personal {
          display: flex;
          justify-content: space-around;
          li {
            flex: 1;
            font-size: 16px;
            padding-top: 12px;
            cursor: pointer;
            color: #333;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            &:hover {
              color: $mainGlod;
              background-color: #f5f5f5;
            }
          }
        }
      }
    }
  }
  .loginBtn {
    width: 115px;
    margin: 15px auto;
    line-height: 30px;
    text-align: center;
    color: $mainGlod;
    background: #333333;
    border: 1px solid #333333;
    cursor: pointer;
    &:hover {
      background: #ffffff;
      border-color: $mainGlod;
    }
  }
  .checkOut {
    color: #fff;
    background-color: #333;
    &:hover {
      background-color: $mainGlod;
    }
  }

}

.consult_b{
  .consult_img{
    width: 100%;
  }
}
>>>.qrcode_dialog .el-dialog__body{
  padding-bottom: 0;
  padding-top: 10px;
}
>>>.pay_dialog{
  border-radius: 40px;
  background: linear-gradient(180deg, #FFE5C4 0%, #FFFFFF 21%);
}
>>>.pay_dialog .el-dialog__body{
  padding-bottom: 0;
  padding-top: 10px;
}
.money_img{
  width: 100%;
}
.pay_content{
  // display: flex;
  // flex-direction: column;
  // justify-content: center;
  text-align: center;
  padding-bottom: 40px;
  .bom_txt{
    margin: 0 auto;
    margin-top: 30px;
    width: 420px;
    font-size: 16px;
    color: #612C08;
    line-height: 22px;
  }
  .pay_but{
    cursor: pointer;
    margin: 0 auto;
    width: 252px;
    height: 50px;
    border-radius: 160px;
    background: linear-gradient(90deg, #865533 0%, #5E2905 100%);
    font-size: 20px;
    font-weight: 500;
    color: #FFF6F0;
    line-height: 50px;
  }
  .pay_text{
    padding: 0 20px 40px;
    display: flex;
    justify-content: space-around;
    .pay_text_item{
      .t{
        margin-top: 15px;
        font-size: 20px;
        font-weight: 500;
        color: #602B08;
      }
      .img{
        width: 70px;
        height: 70px;
      }
    }
  }
  .pay_img{
    width: 200px;
    margin-left: -42px;
  }
  .enter{
    margin-bottom: 40px;
    font-size: 50px;
    font-weight: bold;
    color: #9F480F;
    text-shadow: 0px 4px 2px rgba(222, 86, 0, 0.4);
  }
}
//  .el-dialog__body{
//   padding: 0;
// }
// ::v-deep.topNav .el-dialog .el-dialog__header{
//     padding: 0 !important;
//     height: 100px;
// }
// ::v-deep.el-dialog__header{
//     padding: 0 !important;
//   }
// ::v-deep.consult_dialog{
//     background-color: #c83732;
// }
// ::v-deep .consult_dialog .el-dialog__body{
//     padding: 0 !important;
//   }
</style>
